import redis

from django.http import HttpResponse, JsonResponse
from goods.models import SKU


def test_cors(request):
    """测试跨域"""
    return HttpResponse("九霄龙吟惊天变,cors is ok")


pool = redis.ConnectionPool(host="localhost", port=6379, db=0)
r = redis.Redis(connection_pool=pool)


def stock_view(request):
    """测试Redis分布式锁"""
    # Redis分布式锁[只有1把]
    with r.lock("dashopt:stock", blocking_timeout=5) as lock:
        sku = SKU.objects.get(id=1)
        sku.stock -= 1
        sku.save()

    return JsonResponse({"code": 200})














